package com.muyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.entity.UserEntity;
import com.muyu.mapper.UserMapper;
import com.muyu.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: UserServiceImpl
 * @Author: Zirun.Xin
 * @Date: 2024/12/30 18:58
 * @Version: 1.0
 * @Description: 用户管理业务实现曾
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {

    /**
     * 用户持久层
     */
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询db1的用户数据
     */
    @Override
    public List<UserEntity> getUsers() {
        return this.list();
    }

    /**
     * 查询db2的用户数据
     */
    @Override
    public List<UserEntity> getUsers2() {
        return this.list();
    }


    /**
     * 根据性别查询所有用户
     *
     * @param sex 性别
     */
    @Override
    public List<UserEntity> getUsersBySex(String sex) {
        //条件构造器
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        //eq 代表“ = ”；例如 eq("sex", "男") ---> sex = '男';等同于拼接在sql语句后边的where条件。
        wrapper.eq("sex", sex);
        //将条件带入返回
        List<UserEntity> list = this.list(wrapper);
        //返回数据
//        wrapper.ge("age","11");  =>age>=11
        return list;
    }


    @Override
    public UserEntity getUserById(String id) {
        LambdaQueryWrapper<UserEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserEntity::getId, id);

        wrapper.ge(UserEntity::getAge, 11);
        return this.getOne(wrapper);
    }



    public boolean updateByUserId() {

        return true;
    }


    /**
     * 根据用户id修改用户信息
     */
    public List<UserEntity> getUsersByIdAndDate(Integer userId, Integer age) {
        //lambda条件构造器
        LambdaQueryWrapper<UserEntity> queryWrapper = new LambdaQueryWrapper<>();
        //传入条件
        queryWrapper.eq(UserEntity::getId, userId);
        queryWrapper.eq(UserEntity::getAge, age);
        //调用修改方法
        return this.list(queryWrapper);
    }
}